package Entities;

import java.util.ArrayList;
import Entities.Stock_item;
import digitshoplogic.DBAccessSingleton;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Category {
	public Integer parentId;
	public int categoryId;
	public String cateName;
	public String description;

    public Category(Integer parentId, int categoryId, String cateName, String description) {
        this.parentId = parentId;
        this.categoryId = categoryId;
        this.cateName = cateName;
        this.description = description;
    }


public static final Category getCategoryByID(int cat_id) throws SQLException {
        Category cat = new Category();

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("select * from category where category.categoryId = ?");
         ps.setInt(1, cat_id);

        ResultSet rs = ps.executeQuery();

         while (rs.next()) {
            cat.parentId = rs.getInt(1);
            cat.categoryId = rs.getInt(2);
            cat.cateName = rs.getString(3);
            cat.description = rs.getString(4);
        }
        rs.close();
        return cat;
        }

     public static final void addCategoryByObj(Category cat) throws SQLException {

         PreparedStatement ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("INSERT INTO category VALUES (?,?,?,?)");


         ps.setInt(1, cat.parentId);
         if (cat.parentId == 0){
             ps.setNull(1, cat.parentId);
         }
         ps.setInt(2, cat.categoryId);
         ps.setString(3, cat.cateName);
         ps.setString(4, cat.description);




         ps.executeUpdate();
         ps.close();
        }

 public static final void deleteCategoryByID(int cat_id) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("DELETE FROM category WHERE category.categoryId= ?");
	 ps.setInt(1, cat_id);
         ps.executeUpdate();
         ps.close();
     }
  public static final void updateCategoryByObj(Category cat) throws SQLException{

         PreparedStatement  ps;
         Connection con = DBAccessSingleton.getInstance().getConnection();
         ps = con.prepareStatement("UPDATE category SET "
                 + "category.parentId = ?, "
                 + "category.cateName = ?, "
                 + "category.description = ?  "
                 + "WHERE category.categoryId = ?");

	 ps.setInt(1, cat.parentId);
         ps.setString(2, cat.cateName);
         ps.setString(3,cat.description);
         ps.setInt(4, cat.categoryId);

         ps.executeUpdate();
         ps.close();

     }

    public Category(){
    }

    @Override
    public String toString() {
        return cateName;
    }

    public String getCateName() {
        return cateName;
    }

    public void setCateName(String cateName) {
        this.cateName = cateName;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

}